/*****************************************************************************
 * plum_kernel_shell.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "kernel/include/plum_kernel_api.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
/* thread */
#define PLUM_SHELL_STATCK_SIZE (1024 << 2)
#define PLUM_SHELL_PRIORITY    24
#define PLUM_SHELL_THREAD_NAME (PLUM_CONST plum_s8 *)"shell"

/* attr */
#define PLUM_SHELL_ATTR_MAX_ARGV (5)
#define PLUM_SHELL_ATTR_MAX_LEN  (128)
#define PLUM_SHELL_ARRT_VERSION  "1.0.1"

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/**
 * @brief shell线程参数
 */
typedef struct {
    plum_s8 (*shell_get_char)(plum_void);

    plum_u8 toexit : 1;
    plum_u8 inited : 1;
    plum_u8 reserve : 6;

    plum_s8 cache_cmd[PLUM_SHELL_ATTR_MAX_LEN];
    plum_u8 cursor;
} shell_handle_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE
shell_handle_t shell_hdl;

PLUM_SECTION_REF(PLUM_SHELL_SECTION, plum_shell_t);

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void shell_show_usage(plum_void)
{
    plum_printk(
        "\nWelcome to Plum Shell Interface(%s).\n"
        "\'help\' to list all commanders.\r\n",
        PLUM_SHELL_ARRT_VERSION);
}

PLUM_PRIVATE
plum_void shell_show_commands(plum_u32 argc, plum_s8 **argv)
{
    PLUM_UNUSED(argc);
    PLUM_UNUSED(argv);

    plum_u32 loop;
    if (argc == 1) {
        for (loop = 0;
             loop < PLUM_SECTION_ITEM_COUNT(PLUM_SHELL_SECTION, plum_shell_t);
             loop++) {
            plum_shell_t *item =
                PLUM_SECTION_ITEM_GET(PLUM_SHELL_SECTION, plum_shell_t, loop);

            plum_u32 cmd_len   = strlen(item->cmd);
            plum_u32 shell_len = strlen(argv[0]);
            if ((cmd_len == shell_len) && (strcmp(item->cmd, argv[0]) == 0)) {
                plum_printk("%0.16s -  %s\r\n", item->cmd, item->desc);
                break;
            }
        }
    }
    else {
        for (loop = 0;
             loop < PLUM_SECTION_ITEM_COUNT(PLUM_SHELL_SECTION, plum_shell_t);
             loop++) {
            plum_shell_t *item =
                PLUM_SECTION_ITEM_GET(PLUM_SHELL_SECTION, plum_shell_t, loop);
            plum_printk("%0.16s -  %s\r\n", item->cmd, item->desc);
        }
    }
}
PLUM_SHELL_EXPORT("help", "list all pash commanders", shell_show_commands);

PLUM_PRIVATE
plum_void dev_cli_exit(plum_u32 argc, plum_s8 **argv)
{
    shell_hdl.toexit = 1;
}
PLUM_SHELL_EXPORT("exit", "exit pash system", dev_cli_exit);

/**
 * @brief  从命令行获取各个参数
 *
 * @param[out] argc    -   参数个数
 * @param[out] argv    -   参数table
 * @param[in]  cmd     -   命令行
 *
 * @return:
 * **/
PLUM_PRIVATE
plum_void shell_get_arg(plum_u32 *argc, plum_s8 **argv, plum_s8 *cmd)
{
    plum_s8 *ptr = cmd;
    plum_u8  cnt = 0;

    plum_s8 *start = plum_null;
    do {
        if ((!start) && *ptr && (*ptr != 0x20)) {
            start = ptr;
        }

        if (*ptr == 0x20) {  // space
            *ptr = '\0';
            if (start) {
                argv[cnt++] = start;
            }
            start = plum_null;
        }

        ptr++;
    } while (*ptr != '\0');

    if (start) {
        argv[cnt++] = start;
    }

    *argc = cnt;
}

/**
 * @brief:  执行命令行指令
 *
 * @param[in]:  cmdline     -   指令
 *
 * @return:
 * **/
PLUM_PRIVATE
plum_void shell_run_cmdline(plum_s8 *cmdline)
{
    plum_u32 argc                           = 0;
    plum_s8 *argv[PLUM_SHELL_ATTR_MAX_ARGV] = {plum_null};

    shell_get_arg(&argc, argv, cmdline);
    if (argv[0] == plum_null) {
        return;
    }

    plum_u32 loop;
    for (loop = 0;
         loop < PLUM_SECTION_ITEM_COUNT(PLUM_SHELL_SECTION, plum_shell_t);
         loop++) {
        plum_shell_t *item =
            PLUM_SECTION_ITEM_GET(PLUM_SHELL_SECTION, plum_shell_t, loop);
        plum_u32 shell_size = strlen(item->cmd);
        plum_u32 cmd_size   = strlen(argv[0]);
        if ((shell_size == cmd_size) &&
            (memcmp(item->cmd, argv[0], cmd_size) == 0)) {
            item->handler(argc - 1, argv + 1);
            return;
        }
    }

    plum_printk("Invalid Comander!\r\n");
}

PLUM_PRIVATE
plum_void shell_work_thread(plum_void *arg)
{
    shell_handle_t *hdl = (shell_handle_t *)arg;

    shell_show_usage();

    plum_printk("pash >");
    plum_s8 c = 0;
    while (!hdl->toexit) {
        c = hdl->shell_get_char();
        if (c < 0) {
            plum_kal_thread_delay(1);
        }
        else if (c != '\n') {
            hdl->cache_cmd[hdl->cursor++] = c;
        }
        else {
            shell_run_cmdline(hdl->cache_cmd);  //这里去除了回车
            hdl->cursor = 0;
            memset(hdl->cache_cmd, 0, sizeof(hdl->cache_cmd));
            plum_printk("pash >");
        }
    }

    plum_printk("\r\nPash Exited!!!\r\n");
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_shell_init(plum_s32 (*get_char)(plum_void))
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!shell_hdl.inited) {
            shell_hdl.shell_get_char = get_char;
            shell_hdl.toexit         = 0;

            rc = plum_kal_thread_create(
                PLUM_SHELL_THREAD_NAME, shell_work_thread, &shell_hdl,
                PLUM_SHELL_PRIORITY, PLUM_SHELL_STATCK_SIZE);
            if (rc) {
                plum_printk("create cli thread err,rc:%d\r\n", rc);
                break;
            }
        }
        else {
            plum_printk("shell thread is exsited\r\n");
            rc = PLUM_ECODE_INVALID;
            break;
        }

        shell_hdl.inited = 1;
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_shell_deinit(plum_void)
{
    shell_hdl.toexit = 1;

    return (PLUM_ECODE_OK);
}

PLUM_PUBLIC
plum_s32 plum_shell_suspend(plum_void)
{
    return plum_kal_thread_suspend(PLUM_SHELL_THREAD_NAME);
}

PLUM_PUBLIC
plum_s32 plum_shell_resume(plum_void)
{
    return plum_kal_thread_resume(PLUM_SHELL_THREAD_NAME);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
